package com.jd.ty.service;

import java.util.Date;
import java.util.List;

import com.jd.ty.dao.FunctionDAO;
import com.jd.ty.dao.PermissionDAO;
import com.jd.ty.dao.UserDAO;
import com.jd.ty.dao.constant.QueryConstants;
import com.jd.ty.dao.util.HqlCriterions;
import com.jd.ty.pojo.Permission;
import com.jd.ty.pojo.User;
import com.jd.ty.service.exception.ErrorCode;
import com.jd.ty.service.exception.ErrorMessage;
import com.jd.ty.service.exception.ServiceException;
import com.jd.ty.util.CommonUtils;
import com.jd.ty.util.Pager;

public class UserService {

	private UserDAO userDAO;
	private FunctionDAO functionDAO;
	private PermissionDAO permissionDAO;

	@SuppressWarnings("unchecked")
	public List<User> getList(String filter) {
		return userDAO.getByQuery(filter);
	}

	@SuppressWarnings("unchecked")
	public List<User> getList(String filter, String sort) {
		return userDAO.getByQuery(filter, sort);
	}

	@SuppressWarnings("unchecked")
	public List<User> getList(Pager pager, String filter, String sort) {
		return userDAO.getByQuery(pager, filter, sort);
	}

	public User get(long id) {
		return (User) userDAO.getById(id);
	}

	public User load(long id) {
		User user = (User) userDAO.loadById(id);
		List<Permission> per = user.getRole().getPermissions();
		for (Permission permission : per) {
			userDAO.initialize(permission);
		}
		return user;
	}

	

	public void add(final User user) throws ServiceException {
		HqlCriterions hc = new HqlCriterions();
		hc.and(QueryConstants.User.loginId, user.getLoginId(),
				QueryConstants.EQ);

		if (userDAO.getCountByQuery(hc.getHqlString()) > 0) {
			String errMsg = CommonUtils.formatString(ErrorMessage.DUPLICATION_ENTITY_USER, 
					new String[]{user.getLoginId()});
			throw new ServiceException(ErrorCode.DUPLICATION_ENTITY, errMsg);
		}

		User newUser = new User();
		newUser.setLoginId(user.getLoginId());


		newUser.setCreatedDate(new Date());
		newUser.setLastModified(newUser.getCreatedDate());

		userDAO.saveOrUpdate(newUser);

	}

	public void update(final User user) throws ServiceException {
		HqlCriterions hc = new HqlCriterions();
		hc.and(QueryConstants.User.loginId, user.getLoginId(),
				QueryConstants.EQ);
		hc.and(QueryConstants.User.userId, user.getUserId(), QueryConstants.NE);

		if (userDAO.getCountByQuery(hc.getHqlString()) > 0) {
			String errMsg = CommonUtils.formatString(ErrorMessage.DUPLICATION_ENTITY, 
					new String[]{user.getLoginId()});
			throw new ServiceException(ErrorCode.DUPLICATION_ENTITY, errMsg);
		}

		User newUser = (User) userDAO.loadById(user.getUserId());



		newUser.setLastModified(new Date());
	

	}



	public boolean hasPermission(long id, long functionId) {
		User user = (User) userDAO.getById(id);
		for (Permission permission : user.getRole().getPermissions()) {
			if (functionId == permission.getFunction().getFunctionId()) {
				return true;
			}
		}
		return false;
	}

	public User login(String loginId, String password) throws ServiceException {
		
		HqlCriterions hc = new HqlCriterions();
		hc.and(QueryConstants.User.loginId, loginId, QueryConstants.EQ);
		//hc.and(QueryConstants.User.deleted, false, QueryConstants.EQ);
		User user = (User) userDAO.getOneByQuery(hc.getHqlString(), true);

		
		if (user == null) {
			throw new ServiceException(ErrorCode.USER_NOT_EXIST, ErrorMessage.INCORRECT_PASSWORD);
		}
		if (!user.getLoginPwd().equals(password)) {
			throw new ServiceException(ErrorCode.PWD_NOT_MATCH, ErrorMessage.INCORRECT_PASSWORD);
		}
		userDAO.initialize(user.getRole());
		functionDAO.initialize(user.getRole().getFunctions());
		permissionDAO.initialize(user.getRole().getPermissions());
		return user;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public FunctionDAO getFunctionDAO() {
		return functionDAO;
	}

	public void setFunctionDAO(FunctionDAO functionDAO) {
		this.functionDAO = functionDAO;
	}

	public PermissionDAO getPermissionDAO() {
		return permissionDAO;
	}

	public void setPermissionDAO(PermissionDAO permissionDAO) {
		this.permissionDAO = permissionDAO;
	}

}
